package jbl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jbl.dao.TmenuDaoI;
import jbl.model.Tmenu;
import jbl.model.Tuser;
import jbl.pageModel.Menu;
import jbl.service.MenuServiceI;
import jbl.util.Encrypt;
import jbl.util.JblTool;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("menuService")
public class MenuServiceImpl implements MenuServiceI {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MenuServiceImpl.class);

	private TmenuDaoI menuDao;

	public TmenuDaoI gemenuDao() {
		return menuDao;
	}
	@Autowired
	public void semenuDao(TmenuDaoI menuDao) {
		this.menuDao = menuDao;
	}
	
	@Override
	public List<Menu> getTreeNode(String id) {
		List<Menu> nl=new ArrayList<Menu>();
		try {
			
		String hql=null;
		Map<String, Object>params=new HashMap<String, Object>();
		Tuser user = JblTool.getTuser();
		if(null == user){
			return null;
		}
		if(id==null||id.equals("") ){
			//hql="from Tmenu t where t.tmenu is null";
			hql = 	"SELECT t1 FROM  T_usergroup_menu t,"
					+" Tmenu t1 "
					+" WHERE t.tusergroup.id =:id AND  t1.id = t.tmenu.id order by t1.id asc ";
			params.put("id", user.getTuserGroup().getId());
		} else {
			hql="from Tmenu t where t.tmenu.id=:id";
			params.put("id", id);
		}
		List<Tmenu>l=menuDao.find(hql,params);
		if(l!=null&&l.size()>0){
			for(Tmenu t:l){
				
				Menu m=new Menu();
				BeanUtils.copyProperties(t, m);
				Map<String,Object>attributes=new HashMap<String,Object>();
				m.setPid(id);
				attributes.put("url", t.getUrl());
				m.setAttributes(attributes);	
				Set<Tmenu> s=t.getTmenus();
				if(s!=null&&!s.isEmpty()){
					
					m.setState("closed");//节点以文件夹形式体现
				}else{
					
					m.setState("open");//节点以文件的形式体现
				}
			
				nl.add(m);
			}
			
		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return nl;
	}

	/*
	 * 添加菜单
	 */
	@Override
	public void addNode(Menu tpm) {
		String hql = null;
		String hql_ = null;
		Tmenu Tmenu = new Tmenu();
		Map<String, Object> params = new HashMap<String, Object>();
		Set<Tmenu> Tmenus = new HashSet<Tmenu>();
		if (null != tpm.getPid() && !tpm.getPid().equals("")) {
			String pid = tpm.getPid();
			hql = "from Tmenu t where t.tmenu.id=:pid";
			hql_ = "from Tmenu t where t.id=:pid";
			params.put("pid", pid);
			// 查询该节点下的所有子节点
			List<Tmenu> list = menuDao.find(hql, params);
			// 将该节点下的最后一个子节点的id+1就是当前要添加结点的id

			if (list.size() > 0) {
				String afterId = "";
				afterId = list.get(list.size() - 1).getId().trim();

				String suffix = afterId.substring(afterId.length() - 1,
						afterId.length());

				int parseInt = Integer.parseInt(suffix) + 1;
				String valueOf = String.valueOf(parseInt);
				if (parseInt < 10) {
					Tmenu.setId(afterId.substring(0,
							afterId.length() - 1)
							+ valueOf);
				} else {
					Tmenu.setId(afterId.substring(0,
							afterId.length() - 2)
							+ valueOf);
				}
			} else {
				String parentId = menuDao.get(hql_, params).getId()
						.trim();
				Tmenu.setId(parentId + "00");
			}

			Tmenu Tmenu2 = menuDao.get(hql_, params);
			Tmenu.setTmenu(Tmenu2);
			Tmenus.add(Tmenu);
			Tmenu2.setTmenus(Tmenus);
		} else {
			String hql3 = "from Tmenu t where t.tmenu=null";
			List<Tmenu> l = menuDao.find(hql3);
			int size = l.size();
			String actionMenuId = "0";
			Tmenu.setId(actionMenuId + size);
		}
		Tmenu.setText(tpm.getText());
		Tmenu.setUrl(tpm.getUrl());
		Tmenu.setIconCls(tpm.getIconCls());
		Tmenu.setId(Encrypt.createUUID());

		menuDao.save(Tmenu);
	}
	/**
	 *查询某一节点下有多少子节点
	 */
	public int countChildNode(String id){
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("id", id);
		String hql="from Tmenu where t.tmenu.id=:id";
		Long count = menuDao.count(hql,params);
		return count.intValue();
	}
	
	/*
	 * 获得treeGrid
	 */
	@Override
	public List<Menu> treeGrid(String id) {

		String hql = null;
		Map<String, Object> params = new HashMap<String, Object>();
		List<Menu> l = new ArrayList<Menu>();
		if (id != null && !id.equals("")) {
			hql = "from Tmenu t where t.tmenu.id=:id";
			params.put("id", id);
		} else {
			hql = "from Tmenu t where t.tmenu is null";
		}
		List<Tmenu> list = menuDao.find(hql, params);
		for (Tmenu tmenu : list) {
			Menu pm = new Menu();
			pm.setId(tmenu.getId());
			pm.setText(tmenu.getText());
			pm.setIconCls(tmenu.getIconCls());
			pm.setPid(id);
			pm.setUrl(tmenu.getUrl());
			if (null != tmenu.getTmenu()) {
				pm.setPtext(tmenu.getTmenu().getText());
			}
			if (tmenu.getTmenus().size() > 0) {
				pm.setState("closed");
			} else {
				pm.setState("open");
			}
			l.add(pm);
		}
		return l;
	}

	/**
	 * 修改菜单
	 */
	public void update(Menu tpm) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", tpm.getId());
		String hql = "from Tmenu t where t.id=:id";

		Tmenu tmenu = menuDao.get(hql, params);
		tmenu.setText(tpm.getText());
		tmenu.setIconCls(tpm.getIconCls());
		tmenu.setUrl(tpm.getUrl());
		menuDao.save(tmenu);

	}

	/**
	 * 根据id删除相关菜单,利用递归判断要删除的结点下有没有子节点
	 */
	public Json delete(String id) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "from Tmenu t where t.id=:id";
		params.put("id", id);
		Tmenu tmenu = menuDao.get(hql, params);
		Json j=new Json();
		if(tmenu.getTmenus().size()<=0){
			try {
				menuDao.delete(tmenu);
				j.setMsg("删除"+tmenu.getText()+"成功！");
				j.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
				j.setMsg("删除"+tmenu.getText()+"失败！");
			}
			return j;
		}else{
			j.setSuccess(false);
			j.setMsg("由于\""+tmenu.getText()+"\"结点下有子节点所以删除失败！");
			return j;
		}
	}

	/**
	 * 统计当前菜单下有多少子节点
	 */
	private Long countChildren() {
		/*
		 * Map<String,Object> map=new HashMap<String,Object>(); map.put("pid",
		 * pid);
		 */
		String hql = "select count(*) from Tmenu t where t.tmenu.id = 00";
		return menuDao.count(hql);
	}

	/**
	 * 
	 */
	public List<Menu> getAllMenu() {
		String hql="from Tmenu";
		List<Tmenu> list = menuDao.find(hql);
		List<Menu> l=new ArrayList<Menu>();
		for (Tmenu tmenu : list) {
			Menu tpm=new Menu();
			tpm.setId(tmenu.getId());
			tpm.setText(tmenu.getText());
			l.add(tpm);
		}
		return l;
	}
	@Override
	public List<Menu> comboboxInfo() {
		String hql="from Tmenu t where t.tmenu is null";
		List<Tmenu> list = menuDao.find(hql);
		List<Menu> l=new ArrayList<Menu>();
		for (Tmenu tmenu : list) {
			Menu tpm=new Menu();
			tpm.setId(tmenu.getId());
			tpm.setText(tmenu.getText());
			l.add(tpm);
		}
		return l;
	}
}
